package reporting;

import java.io.*;

public class FileWork {
	
	public static FileWork getInstance() {
	      return INSTANCE;
	}
	
    public Boolean createFile(String path) {
    	try {
    		File file = new File(path);
    		// Create file if it does not exist
    		boolean success = file.createNewFile();
    		return success;    		
        } catch (IOException e) {
        	System.err.println("Error: " + e.getMessage());
        	return false;
        }
    }
    
	public Boolean writeFile(String path, String text) {
		try {
			Writer output = null;
			File file = new File(path);
			output = new BufferedWriter(new FileWriter(file));
			output.write(text);
			output.close();
			return true;
		} catch (IOException e) {
			System.err.println("Error: " + e.getMessage());
			return false;
		}
    }
    
    public void appendFile(String path, String text) {
        try {
            // Create file 
            FileWriter fstream = new FileWriter(path,true);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(text);
            //Close the output stream
            out.close();
        } catch(Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
    
    public String readFile(String path) {
    	String result = "";
    	try {
    		// Open the file that is the first 
    		// command line parameter
    		FileInputStream fstream = new FileInputStream(path);
        	// Get the object of DataInputStream
        	DataInputStream in = new DataInputStream(fstream);
        	BufferedReader br = new BufferedReader(new InputStreamReader(in));
        	String strLine;
        	//Read File Line By Line
        	while ((strLine = br.readLine()) != null) {
        		result = result.concat(strLine);
        	}
        	//Close the input stream
        	in.close();
         
        } catch (Exception e) {
        		//Catch exception if any
        		System.err.println("Error: " + e.getMessage());
        }
        	return result;
	}
    
    public Boolean deleteFileOrDir(String path) {
        File f1 = new File(path);
        boolean success = f1.delete();
        if (!success) {
        	System.exit(0);
        	return true;
        } else {
        	return false;
        }
    }
    
    public Boolean createDir(String path) {
    	Boolean result = false;
    	try {
    		// Create one directory
    		Boolean success = (new File(path)).mkdir();
    		if (success) {
    			result = true;
    		}    
    	} catch (Exception e) {
    		//Catch exception if any
    		System.err.println("Error: " + e.getMessage());
    	}
    	return result;
   	}
    
    public void createDirs(String path) {
    	try {
    		// Create multiple directories
    		boolean success = (new File(path)).mkdir();    		
    		success = (new File(path)).mkdirs();
    		if (success) {
    			System.out.println("Directories: " + path + " created");
    		}
    	} catch(Exception e) {
    		//Catch exception if any
    		System.err.println("Error: " + e.getMessage());
    	}
   	}
    
    public Boolean existsFileOrDir(String path) {
        File f1 = new File(path);
        if(f1.exists()) {
            return true;
        } else {
        	return false;
        }
   }
    
    private static final FileWork INSTANCE = new FileWork();
}